package com.lockmem.listener;

import com.lockmem.Conf.ConfHelper;
import com.lockmem.Conf.ConfKey;
import com.lockmem.study.StudyActivity;

import android.app.KeyguardManager;
import android.app.Service;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.content.SharedPreferences;

@SuppressWarnings("deprecation")
public class ListenerService extends Service {
	private BroadcastReceiver receiver;
	IntentFilter filter;
	private final String tag = "ListenerService";
	private SharedPreferences pref = null;

	private KeyguardManager mKeyguardManager;
	private KeyguardLock mKeyguardLock;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	private boolean getSwitch() {
		if (pref == null)
			pref = this.getSharedPreferences(ConfHelper.SharePrefName,
					MODE_PRIVATE);
		return pref.getBoolean(ConfKey.Switch, true);
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		receiver = new Screen_on_receiver();
		filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
		filter.addAction(Intent.ACTION_SCREEN_ON);
		registerReceiver(receiver, filter);
		PhoneStateListener phonelistener = new PhoneStateListener() {

			@Override
			public void onCallStateChanged(int state, String incomingNumber) {
				// TODO Auto-generated method stub
				switch (state) {
				case TelephonyManager.CALL_STATE_IDLE:
					isCalling = false;
					break;
				case TelephonyManager.CALL_STATE_OFFHOOK:
				case TelephonyManager.CALL_STATE_RINGING:
					isCalling = true;
					break;
				}
				super.onCallStateChanged(state, incomingNumber);
			}

		};
		TelephonyManager manager = (TelephonyManager) this
				.getSystemService(this.TELEPHONY_SERVICE);
		manager.listen(phonelistener, PhoneStateListener.LISTEN_CALL_STATE);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		// this.startService(new Intent(this,ListenerService.class));
		// if(isOn)
		unregisterReceiver(receiver);
		// mKeyguardLock.reenableKeyguard();
		startService(new Intent(ListenerService.this, ListenerService.class));
		super.onDestroy();
	}

	private void initScreenLock() {
		mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
		mKeyguardLock = mKeyguardManager.newKeyguardLock("");
	}

	private boolean isCalling = false;

	public class Screen_on_receiver extends BroadcastReceiver {
		private String tag = "Screen_on_receiver";

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String act = intent.getAction();
			if (getSwitch() && (!isCalling)) {
				if (act.equals(Intent.ACTION_SCREEN_ON)) {
					initScreenLock();
					mKeyguardLock.disableKeyguard();
				}
				Intent in = new Intent(context, StudyActivity.class);
				in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(in);
			}
		}
	}

}
